﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ImportDataCore
{
    public partial class UCCheck : DevExpress.XtraEditors.XtraUserControl
    {

        public delegate  void AfterSetPathEventHandler(string filePath);

        public event RegisterLogToLogCenterEventHandler RegisterLogToLogCenter;
        public event AfterSetPathEventHandler AfterSetPathEvent;

        UCLog _ucLog = null;
        string _filePath = string.Empty;
        /// <summary>
        /// 数据路径
        /// </summary>
        public string FilePath
        {
            get { return _filePath; }
        }

        public UCCheck()
        {
            InitializeComponent();
            _ucLog = new UCLog();
            _ucLog.Dock = DockStyle.Fill;
            this.grpLog.Controls.Add(_ucLog);

            this.btnDataPath.Properties.ReadOnly = true;
        }

        public void Clearlog()
        {
            this._ucLog.ClearLog();
        }

        private void btnDataPath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Excel(2003)|*.xls|Excel(2007)|*.xlsx";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                _filePath = dlg.FileName;
                this.btnDataPath.Text = _filePath;

                if (AfterSetPathEvent != null)
                    AfterSetPathEvent(_filePath);
            }
        }

        public void InitProgress(int max, int min, int step)
        {
            this.progressBarCheck.Properties.Maximum = max;
            this.progressBarCheck.Properties.Minimum = min;
            this.progressBarCheck.Properties.Step = step;
            this.progressBarCheck.Position = this.progressBarCheck.Properties.Minimum;
            this.progressBarCheck.Refresh();
        }

        public void NotifyProgress(int position, string log)
        {
            if (position < this.progressBarCheck.Properties.Maximum
                || position > this.progressBarCheck.Properties.Minimum)
            {
                this.progressBarCheck.Position = position;
            }
            if (!string.IsNullOrEmpty(log))
            {
                this.lableTip.Text = "当前正在进行：" + log;
            }
            Application.DoEvents();
        }

        public void NotifyLogInfo(LogEventArgs logEventArgs)
        {
            if (logEventArgs == null)
                return;

            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("\r\n");
            strBuilder.Append("............................");

            strBuilder.Append("\r\n");
            strBuilder.Append("当前时间：" + DateTime.Now.ToString());

            strBuilder.Append("\r\n");
            strBuilder.Append("当前时间：" + DateTime.Now.ToString());

            if (logEventArgs.IRowNum != -1)
            {
                strBuilder.Append("\r\n");
                strBuilder.Append("当前行：" + logEventArgs.IRowNum.ToString());
            }

            if (logEventArgs.IColNum != -1)
            {
                strBuilder.Append("\r\n");
                strBuilder.Append("当前列：" + logEventArgs.IColNum.ToString());
            }

            if (!string.IsNullOrEmpty(logEventArgs.SColName))
            {
                strBuilder.Append("\r\n");
                strBuilder.Append("列名：" + logEventArgs.SColName.ToString());
            }

            if (!string.IsNullOrEmpty(logEventArgs.LogMsg))
            {
                strBuilder.Append("\r\n");
                strBuilder.Append("当前信息：" + logEventArgs.LogMsg);
            }

            strBuilder.Append("\r\n");
            strBuilder.Append("............................");

            this._ucLog.AddLog(strBuilder);
            RegisterLogArgsToLogInfo(logEventArgs, EnumOperaterType.check);
        }

        private void RegisterLogArgsToLogInfo(LogEventArgs logEventArgs, EnumOperaterType operaterType)
        {
            if (RegisterLogToLogCenter != null)
                RegisterLogToLogCenter(logEventArgs, operaterType);
        }


    }
}
